﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ProcessFile
{
    class ConvertAD144StkRow
    {
        private string _line;
        public int LayerNo { set; get; }
        public bool IsTopLayer { set; get; }
        public bool IsBottomLayer { set; get; }
        public bool IsTopSolder { set; get; }
        public bool IsBottomSolder { set; get; }
        public ConvertAD144StkRow(string line)
        {
            _line = line;
        }
        public  bool IsDielectric
        {
            get
            {
                string type = GetDielectricType(_line);
                return !string.IsNullOrEmpty(type);
            }
        }
        public  string GetOnOf(string subStackId)
        {
            string pattern = @"{" + subStackId + @"}CONTEXT=(?<onoroff>\d{1})";
            Regex regex = new Regex(pattern);
            Match match = regex.Match(_line);
            if (match.Success)
            {
                return match.Groups["onoroff"].Value;
            }
            return "0";

        }
        public string GetLayerType()
        {
            string layerType = "";
            string type = GetDielectricType(_line);
            if (string.IsNullOrEmpty(type))
            {
                //check sign or planer
                if (_line.IndexOf("COMPONENTPLACEMENT")>0)
                {
                    layerType = Common.Signal;
                }
                else
                {
                    layerType = Common.Plane;
                }
            }
            else
            {
                switch (type)
                {
                    case "1":
                        layerType = Common.Core;
                        break;
                    case "2":
                        layerType = Common.Prepreg;
                        break;
                    case "3":
                        layerType = Common.Solder;
                        break;
                }
            }
            return layerType;
        }
        public override string ToString()
        {
            string layerNo = LayerNo > 0 ? LayerNo.ToString() : "";
            
            string layerType = GetLayerType();
            if (IsTopSolder)
            {
                layerType = "Soldermask";
            }
            if (IsBottomSolder)
            {
                layerType = "Soldermask";
            }

            string layerName = GetLayerName(_line);
            string materialType = GetDielectricMaterial(_line);
            if (!IsDielectric)
            {
                materialType = "Conductive";
            }
            string dielectriCons = GetDielectricConst(_line);
            string height = GetDielectricHeight(_line);
            string copthink = GetDielectricCopThick(_line);
            
            string content = string.Format("{0}^{1}^{2}^{3}^{4}^{5}^{6}^^^^^^^", layerNo, layerType, layerName, materialType, dielectriCons,height,copthink);
            if (layerType==Common.Signal)
            {
                content = string.Format("{0}^{1}^{2}^{3}^{4}^{5}^{6}^4^4^^^^^", layerNo, layerType, layerName, materialType, dielectriCons, height, copthink);
            }
            return content;

        }

        public  string [] Item
        {
            get { return _line.Split(Common.SplipCharAD14); }
        }
        
        public static string GetLayerName(string input)
        {
            string pattern = @"NAME=(?<layername>[A-z|0-9|\s]*)\|";
            Regex regex = new Regex(pattern);
            Match match = regex.Match(input);
            if (match.Success)
            {
                return match.Groups["layername"].Value;
            }
            return "";
        }
        public static string GetDielectricType(string input)
        {
            string pattern = @"DIELTYPE=(?<layertype>[A-z|0-9|\s]*)\|";
            Regex regex = new Regex(pattern);
            Match match = regex.Match(input);
            if (match.Success)
            {
                return match.Groups["layertype"].Value;
            }
            return "";
        }
        public static string GetDielectricConst(string input)
        {
            string pattern = @"DIELCONST=(?<layerconst>[A-z|0-9|\s|\.]*)\|";
            Regex regex = new Regex(pattern);
            Match match = regex.Match(input);
            if (match.Success)
            {
                return match.Groups["layerconst"].Value;
            }
            return "";
        }
        public static string GetDielectricCopThick(string input)
        {
            string pattern = @"COPTHICK=(?<height>[0-9|\s|\.]*)";
            Regex regex = new Regex(pattern);
            Match match = regex.Match(input);
            if (match.Success)
            {
                return match.Groups["height"].Value;
            }
            return "";
        }
        public static string GetDielectricHeight(string input)
        {
            string pattern = @"DIELHEIGHT=(?<height>[0-9|\s|\.]*)";
            Regex regex = new Regex(pattern);
            Match match = regex.Match(input);
            if (match.Success)
            {
                return match.Groups["height"].Value;
            }
            return "";
        }
        public static string GetDielectricMaterial(string input)
        {
            string keyword="DIELMATERIAL=";
            int start = input.IndexOf(keyword);
            if (start > 0)
            {
                int endIndexOf = input.IndexOf(Common.SplipCharAD14, start);
                if (endIndexOf > start)
                {
                    return input.Substring(start + keyword.Length, endIndexOf - start - keyword.Length).Trim();
                }
            }
           
            return "";
        }
        
    }
}
